
#include	"SysConfig.h"
#include	"Define.h"
#ifdef  HERO
#include	"Audio_6635.h"
#endif
#include	"Port.h"
#include	"Math.h"
#include	"NXP_I2C.h"

#ifdef  NXP

unsigned char ucNXP_I2CData[128];

#define	NXP_STRETCHING
#ifdef  ATOMIC
#undef	NXP_STRETCHING
#endif


void NXP_I2C_Delay(unsigned char ucCnt)
{
	unsigned char i;

	for(i = 0; i < ucCnt; ++i)
	{
	}
}

void NXP_I2C_Start(void)
{
	PO_NXP_SDA = 0;
	NXP_I2C_Delay(2);
	PO_NXP_SCL = 0;
	NXP_I2C_Delay(2);
}

void NXP_I2C_ReStart(unsigned char ucDelay)
{
	PO_NXP_SDA = 1;
	NXP_I2C_Delay(2);
	PO_NXP_SCL = 1;
	NXP_I2C_Delay(2 + ucDelay);
	NXP_I2C_Start();
}

unsigned char NXP_I2C_WriteByte(unsigned char ucData)
{
	unsigned char i;
	unsigned char ucAck = 0;	

	for(i = 0; i < 8; ++i)
	{
		if  (ucData & 0x80)
		{
			PO_NXP_SDA = 1;
		}
		else
		{
			PO_NXP_SDA = 0;
		}
		ucData = ucData << 1;
		NXP_I2C_Delay(1);
		PO_NXP_SCL = 1;
		NXP_I2C_Delay(1);
		PO_NXP_SCL = 0;
	}

	PO_NXP_SDA = 0;
	PD_NXP_SDA = 0;
	NXP_I2C_Delay(1);
	PO_NXP_SCL = 1;
	NXP_I2C_Delay(1);

	if  (PO_NXP_SDA == 1)
	{
		ucAck = 1;
	}
	PD_NXP_SDA = 1;
	PO_NXP_SDA = 0;
	PO_NXP_SCL = 0;
	NXP_I2C_Delay(0);

	return(ucAck);
}

unsigned char NXP_I2C_ReadByte(unsigned char ucAck)
{
	unsigned char ucData = 0x00;
	unsigned char i;

	PD_NXP_SDA = 0;	// IN

	for(i = 0;i < 8; ++i)
	{
		PO_NXP_SCL = 1;
		ucData = (ucData << 1) & 0xFE;
		NXP_I2C_Delay(2);
		if  (PO_NXP_SDA == 1)
		{
			ucData = ucData | 0x01;
		}
		PO_NXP_SCL = 0;
		NXP_I2C_Delay(1);
	}
#if  1
	NXP_I2C_Delay(2);
	PD_NXP_SDA = 1;	// OUT
	if  (ucAck == 0x01)
	{
		PO_NXP_SDA = 1;
	}
	else
	{
		PO_NXP_SDA = 0;
	}
	NXP_I2C_Delay(2);
	PO_NXP_SCL = 1;	
	NXP_I2C_Delay(2);
	PO_NXP_SCL = 0;
	PO_NXP_SDA = 0;
	NXP_I2C_Delay(1);
#endif
	return(ucData);
}

void NXP_I2C_Stop(void)
{
	NXP_I2C_Delay(1);
	PO_NXP_SCL = 1;
	NXP_I2C_Delay(2);
	PO_NXP_SDA = 1;
}

void NXP_I2C_Send(unsigned char *ucBuf, unsigned char ucCnt)
{
	unsigned char i;
	
	#if defined( HERO_EXTERN ) || defined(ATOMIC_EXTERN)
	i = 0;
	if  (i == 0)
	{
		return;
	}
	#endif
	
	NXP_I2C_Start();
	
	NXP_I2C_WriteByte(NXP_CHIP_ADDR | NXP_I2C_WRITE);

	for(i = 0; i < ucCnt; ++i)
	{
		NXP_I2C_WriteByte(*ucBuf);
		++ucBuf;
	}
	NXP_I2C_Stop();
}

void NXP_I2C_Read(unsigned char *ucBuf, unsigned char ucCnt)
{
	unsigned char i;
		
	#if defined( HERO_EXTERN ) || defined(ATOMIC_EXTERN)
	i = 0;
	if  (i == 0)
	{
		return;
	}
	#endif
	
	NXP_I2C_Start();
	
	NXP_I2C_WriteByte(NXP_CHIP_ADDR | NXP_I2C_READ);

#ifdef  NXP_STRETCHING
	PD_NXP_SCL = 0;
	while(1)
	{
		if  (PO_NXP_SCL == 1)
		{
			break;
		}
		NXP_I2C_Delay(1);
	}
	PD_NXP_SCL = 1;
#else
	NXP_I2C_Delay(3);
#endif

	for(i = 0; i < ucCnt; ++i)
	{
		if  (i == (ucCnt - 1))
		{
			*ucBuf = NXP_I2C_ReadByte(1);
		}
		else
		{
			*ucBuf = NXP_I2C_ReadByte(0);
		}
		++ucBuf;
#if	0//def  NXP_STRETCHING
		PD_NXP_SCL = 0;
		while(1)
		{
			if  (PO_NXP_SCL == 1)
			{
				break;
			}
			NXP_I2C_Delay(1);
		}
		PD_NXP_SCL = 1;
#endif
	}
	NXP_I2C_Stop();
}


void NXP_I2C_SendRead(unsigned char *ucBuf, unsigned char ucWCnt, unsigned char ucRCnt)
{
	unsigned char i;
		
	#if defined( HERO_EXTERN ) || defined(ATOMIC_EXTERN)
	i = 0;
	if  (i == 0)
	{
		return;
	}
	#endif
	
	NXP_I2C_Start();
	
	NXP_I2C_WriteByte(NXP_CHIP_ADDR | NXP_I2C_WRITE);

	for(i = 0; i < ucWCnt; ++i)
	{
		NXP_I2C_WriteByte(*(ucBuf + i));
	}

	NXP_I2C_ReStart(0);
	
	NXP_I2C_WriteByte(NXP_CHIP_ADDR | NXP_I2C_READ);
	
#ifdef  NXP_STRETCHING
	PD_NXP_SCL = 0;
	while(1)
	{
		if  (PO_NXP_SCL == 1)
		{
			break;
		}
		NXP_I2C_Delay(1);
	}
	PD_NXP_SCL = 1;
#else
	NXP_I2C_Delay(3);
#endif

	for(i = 0; i < ucRCnt; ++i)
	{
		if  (i == (ucRCnt - 1))
		{
			*ucBuf = NXP_I2C_ReadByte(1);
		}
		else
		{
			*ucBuf = NXP_I2C_ReadByte(0);
		}
		++ucBuf;
#if	0//def  NXP_STRETCHING
		PD_NXP_SCL = 0;
		while(1)
		{
			if  (PO_NXP_SCL == 1)
			{
				break;
			}
			NXP_I2C_Delay(1);
		}
		PD_NXP_SCL = 1;
#endif
	}
	NXP_I2C_Stop();
}

#endif

